﻿using System.Text;
using System.Web.Mvc;
using OpusSuite.Models;

namespace OpusSuite.Controllers
{
    public class SharedController : Controller
    {
        public PartialViewResult ApplicationMessages(string model="")
        {
            return PartialView(model);
        }

        [HttpGet]
        public JsonResult ApplicationMessagesData(int showToUser=1)
        {
            int count = 0;
            var model = new OpusSuiteModel(Utility.WebUtil.ServerTypeFromCookie(), Utility.WebUtil.ConnectionStringFromCookie());

            StringBuilder jsonData = new StringBuilder("{ \"results\": [");
            foreach (var item in model.GetApplicationMessages(true))
            {
                if (count > 0)
                    jsonData.Append(",");

                jsonData.Append("{ ");
                jsonData.AppendFormat("\"{0}\": \"{1}\"", "id", item.Id);
                jsonData.AppendFormat(",\"{0}\": \"{1}\"", "message", item.Message);
                jsonData.AppendFormat(",\"{0}\": \"{1}\"", "level", item.Level);
                jsonData.AppendFormat(",\"{0}\": \"{1}\"", "description", item.Description);
                jsonData.AppendFormat(",\"{0}\": \"{1}\"", "messageDate", item.MessageDate.ToShortDateString());
                jsonData.AppendFormat(",\"{0}\": \"{1}\"", "messageTime", item.MessageDate.ToShortTimeString());
                jsonData.AppendFormat(",\"{0}\": \"{1}\"", "seconds", item.Timeout);
                jsonData.Append(" }");
                count++;
            }
            jsonData.Append(" ] }");
            
            return Json(jsonData.ToString(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AcceptApplicationMessage(int id)
        {
            if (id > 0)
            {
                var model = new OpusSuiteModel(Utility.WebUtil.ServerTypeFromCookie(), Utility.WebUtil.ConnectionStringFromCookie());
                model.AcceptApplicationMessage(id);
            }

            return Json(true);
        }

        //[HttpGet]
        //public JsonResult ValidationMessagesData(int showToUser = 1)
        //{
        //    int count = 0;
        //    var model = new OpusSuiteModel(Utility.WebUtil.ServerTypeFromCookie(), Utility.WebUtil.ConnectionStringFromCookie());

        //    StringBuilder jsonData = new StringBuilder("{ \"results\": [");
        //    foreach (var item in model.ValidationMessages())
        //    {
        //        if (count > 0)
        //            jsonData.Append(",");

        //        jsonData.Append("{ ");
        //        jsonData.AppendFormat("\"{0}\": \"{1}\"", "id", item.Id);
        //        jsonData.AppendFormat(",\"{0}\": \"{1}\"", "message", item.Message);
        //        jsonData.AppendFormat(",\"{0}\": \"{1}\"", "level", item.Level);
        //        jsonData.AppendFormat(",\"{0}\": \"{1}\"", "description", item.Description);
        //        jsonData.AppendFormat(",\"{0}\": \"{1}\"", "messageDate", item.MessageDate.ToShortDateString());
        //        jsonData.AppendFormat(",\"{0}\": \"{1}\"", "messageTime", item.MessageDate.ToShortTimeString());
        //        jsonData.AppendFormat(",\"{0}\": \"{1}\"", "seconds", item.Timeout);
        //        jsonData.Append(" }");
        //        count++;
        //    }
        //    jsonData.Append(" ] }");

        //    return Json(jsonData.ToString(), JsonRequestBehavior.AllowGet);
        //}

        public PartialViewResult OpusMessageDialog(string message, bool btnOk, bool btnCancel)
        {
            ViewBag.Message = message;
            ViewBag.btnOK = btnOk ? "btnOK" : "";
            ViewBag.btnCancel = btnCancel ? "btnCancel" : "";

            return PartialView();
        }

        public PartialViewResult Breadcrumbs()
        {
            return PartialView();
        }
    }
}
